package com.xquant.common.engine.xPP.request.instrument;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xquant.common.engine.xPP.request.Instrument;
import com.xquant.common.engine.xPP.request.scene.Factor;

/**
 * @author B946
 * @date 26/09/2020 14:21
 * @description:
 */
public class Barrier extends AbstractProduct {

    @Override
    public Instrument replaceTerm(Instrument instrument, String valueType, String term){
        super.replaceTerm(instrument, valueType, term);
        replaceExerciseDates(instrument,valueType,term);
        return instrument;
    }

    protected Instrument replaceExerciseDates(Instrument instrument,String valueType,String term){
        if(!Factor.VALUE_TYPE_VALUE.equals(valueType)){
            return instrument;
        }
        JSONArray customLegs = instrument.getTermsheet().getJSONArray("customLegs");
        JSONObject exercise = customLegs.getJSONObject(0).getJSONObject("exercise");
        JSONArray dates = exercise.getJSONArray("dates");
        dates.clear();
        dates.add(instrument.getTermsheet().getString("terminationDate"));
        customLegs.getJSONObject(0).getJSONArray("underlyers").getJSONObject(0).remove("fixingDate");
        return instrument;
    }

    @Override
    public Instrument replaceStrike(Instrument instrument,String valueType,String strike){
        super.replaceStrike(instrument, valueType, strike);
        JSONArray customLegs = instrument.getTermsheet().getJSONArray("customLegs");
        if(Factor.VALUE_TYPE_VALUE.equals(valueType)){
            customLegs.getJSONObject(0).remove("strikePercentage");
            customLegs.getJSONObject(0).put("strike",strike);
        }
        return instrument;
    }

    @Override
    public Instrument replaceStrikePercentage(Instrument instrument,String valueType,String strikePercentage){
        super.replaceStrikePercentage(instrument, valueType, strikePercentage);
        JSONArray customLegs = instrument.getTermsheet().getJSONArray("customLegs");
        if(Factor.VALUE_TYPE_VALUE.equals(valueType)){
            customLegs.getJSONObject(0).remove("strike");
            customLegs.getJSONObject(0).put("strikePercentage",strikePercentage);
        }
        return instrument;
    }



}
